/*
 * @Author: Kilims Ye
 * @LastEditors: Zhijie Luo
 * @Date: 2020-07-01 12:03:01
 * @LastEditTime: 2021-02-24 16:16:26
 * @Description:
 */
import { Toast } from "antd-mobile"
import api from "@api";
import Loader from "@components/Loader";

export const lastFourBankCardNo = (value) => {
  const withoutSpaceNo = value.split(" ").join("");
  return withoutSpaceNo.substring(withoutSpaceNo.length - 4, withoutSpaceNo.length);
};

export const hidePhoneNoWithStar = (phoneNo) => {
  const reg = /(\d{3})\d{4}(\d{4})/;
  return String(phoneNo).replace(reg, "$1****$2");
};

export const hideLastTwoCharsOfName = (name) => {
  let result = String(name)[0];
  for (let i = 0; i < String(name).length - 1; i++) {
    result += "*";
  }
  return result;
};

export const hideMiddleOfIdCardNo = (idNo) => {
  const reg = /^(.{6})(?:\d+)(.{4})$/;
  return String(idNo).replace(reg, "$1******$2");
};

export const hideEmail = (email) => {
  const reg = /^(?:.+)(.{1})@(.+)$/;
  return String(email).replace(reg, "****$1@$2");
};

export const normalizeAmount = (value) => {
  // format to normal amount
  if (value && value.length > 0) {
    if (value.indexOf(".") !== value.length - 1) {
      return JSON.parse(value.split(",").join(""));
    } else {
      return JSON.parse(
        value
          .substr(0, value.length - 1)
          .split(",")
          .join("")
      );
    }
  } else {
    return "";
  }
};

//Input 时候使用 ： 千分位 + 两位小数点
export function inpuToThousand(num, tofixed) {
  let fixed = tofixed ? tofixed : 2;
  if (num == "") {
    return "";
  } else {
    if (num.indexOf(".") > 0) {
      return (
        parseInt(num.split(".")[0].split(",").join(""))
          .toString()
          .replace(/\d(?=(\d{3})+$)/g, "$&,") +
        "." +
        num.split(".")[1].substring(0, fixed)
      ); // 小数位数，默认两位
    } else {
      return parseInt(num.split(",").join(""))
        .toString()
        .replace(/\d(?=(\d{3})+$)/g, "$&,");
    }
  }
}

//固定数字 千分位 + 小数
export function toThousand(num, tofixed) {
  try {
    let fixed = tofixed ? tofixed : 2;
    let afterNumber = Number(num).toFixed(fixed);
    if (afterNumber == "") {
      return "";
    } else {
      if (afterNumber.indexOf(".") > 0) {
        return (
          parseInt(afterNumber.split(".")[0])
            .toString()
            .replace(/\d(?=(\d{3})+$)/g, "$&,") +
          "." +
          afterNumber.split(".")[1]
        );
      } else {
        return parseInt(afterNumber)
          .toString()
          .replace(/\d(?=(\d{3})+$)/g, "$&,");
      }
    }
  } catch (error) {
    Toast.fail("传入错误数字");
  }
}

export function insertStr(soure, position, newStr) {
  return soure.slice(0, position) + newStr + soure.slice(position);
}

export function phoneFormatter(phoneNo) {
  return insertStr(insertStr(phoneNo, 3, " "), 8, " ");
}

//只允许输入数字
export function numberOnly(value) {
  return value.replace(/[^\d]/g, "");
}
// 格式化时间为 ：2017-10-20 12:23
export function formatDate(date) {
  /* eslint no-confusing-arrow: 0 */
  const pad = (n) => (n < 10 ? `0${n}` : n);
  const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
  const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
  return `${dateStr} ${timeStr}`;
}
export function formatPayType(type) {

  api
    .getEnumListData({
      enumerationNameList: ["PayInterestType"],
    })
    .then((res) => {
      // Toast.hide();
      const { data } = res

      let currentType = data[0].values
      currentType.forEach((item) => {
        if (item.name === type) {
          // console.log("item.description",item.description)
          return item.description
        }
      })

      // console.log("枚举的数据", data[0].values);
      // console.log("得到的处理后的数据", type)
      // console.log("得到的处理后的数据", currentType)
    });

  switch (type) {
    case "USER_PAY":
      return "用户付息"
      break;
    case "MERCHANT_PAY":
      return "商户付息"
      break;

  }
}